<?php
namespace app\home\controller;
use think\Loader;
use app\home\model\RecordModel;
use app\home\model\CommonModel;
use think\Controller;
use think\Request;

class Record extends Controller
{
    public function __construct(Request $request = null)
    {
        $result = $this->_getBid();
        if(!$result){
            $this->success('未登录', 'index/login');
        }
    }
    public function index()
    {

        $bid = $this->_getBid();
        $field = 'orderid,order_no,money,paytime';
        list($result,$num,$money) = RecordModel::instance()->recordlist($bid, $field);
     //   var_dump($result);exit;
//        for($i=0;$i<count($result);$i++){
//            if($result[$i]['status']==2){
//                $result[$i]['status']='代发货';
//            }elseif($result[$i]['status']==3){
//                $result[$i]['status']='待收货';
//            }elseif($result[$i]['status']==4){
//                $result[$i]['status']='已完成';
//            }elseif($result[$i]['status']==-1){
//                $result[$i]['status']='交易关闭';
//            }
//        }
        
        return view('record/index', ['data' => $result,'num'=>$num,'money'=>round($money,2)]);
    }

    /**
     * 查询
     */
    public function recordfind(Request $request){
        $time = $request->get('data_days');
        $bid = $this->_getBid();
        $data = [];
        list($result,$num,$money) = RecordModel::instance()->recordfind($time,$bid);
        $res = input('param.datas');
        if($res){
            $field = 'orderid,order_no,money,paytime';
            if(strlen($time)<12){
                $data = db('goods_order')->where('bid', $bid)->where('status','neq','1')->where('paytime','>',strtotime($time))->where('paytime','<',(strtotime($time)+24*60*60))->field($field)->order('paytime desc')->select();
            }else{
                $date = explode('至',$time);
                $data = db('goods_order')->where('bid', $bid)->where('status','neq','1')->where('paytime','>',strtotime($date[0]))->where('paytime','<',(strtotime($date[1])+24*60*60))->field($field)->order('paytime desc')->select();
            }
            //数据库查询
            $path = dirname(__FILE__); //找到当前脚本所在路径
            Loader::import('PHPExcel.Classes.PHPExcel');
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
            $PHPExcel = new \PHPExcel();
//        $PHPExcel_IOFactory = new PHPExcel_IOFactory();
            $PHPSheet = $PHPExcel->getActiveSheet();
            $PHPSheet->setTitle("demo"); //给当前活动sheet设置名称
            $PHPSheet->setCellValue("A1", "ID")
                ->setCellValue("B1", "订单号")
                ->setCellValue("C1", "支付金额")
                ->setCellValue("D1", "支付时间");

            //       var_dump(count($data));
            //       exit;
            $d=2;
            for($i=0;$i<count($data);$i++){
                $time =date('Y-m-d H:i:s',$data[$i]['paytime']);
                $PHPSheet->setCellValue("A" . $d, $data[$i]['orderid'])
                    ->setCellValue("B" . $d, $data[$i]['order_no'])
                    ->setCellValue("C" . $d, $data[$i]['money'])
                    ->setCellValue("D" . $d, $time);
                $d++;
            }
//        exit;
//        $PHPSheet->setCellValue("A2","张三")->setCellValue("B2","2121");//表格数据
            $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
            ob_end_clean(); // Added by me
            ob_start(); // Added by me
            header('Content-Disposition: attachment;filename="表单数据.xlsx"');
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
        }
        return view('record/index', ['data' => $result,'num'=>$num,'money'=>round($money,2)]);

    }

    /**
     * 导出
     */
    public function excel()
    {
        $data = db('goods_order')->where('status','NEQ', 1 )->field('paytime,orderid,order_no,money')->order('orderid desc')->select();
        //数据库查询
        $path = dirname(__FILE__); //找到当前脚本所在路径
        Loader::import('PHPExcel.Classes.PHPExcel');
        Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        $PHPExcel = new \PHPExcel();
//        $PHPExcel_IOFactory = new PHPExcel_IOFactory();
        $PHPSheet = $PHPExcel->getActiveSheet();
        $PHPSheet->setTitle("demo"); //给当前活动sheet设置名称
        $PHPSheet->setCellValue("A1", "ID")
            ->setCellValue("B1", "订单号")
            ->setCellValue("C1", "支付金额")
            ->setCellValue("D1", "支付时间");

        //       var_dump(count($data));
        //       exit;
        $d=2;
        for($i=0;$i<count($data);$i++){
            $time =date('Y-m-d H:i:s',$data[$i]['paytime']);
            $PHPSheet->setCellValue("A" . $d, $data[$i]['orderid'])
                ->setCellValue("B" . $d, $data[$i]['order_no'])
                ->setCellValue("C" . $d, $data[$i]['money'])
                ->setCellValue("D" . $d, $time);
            $d++;
        }
//        exit;
//        $PHPSheet->setCellValue("A2","张三")->setCellValue("B2","2121");//表格数据
        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        ob_end_clean(); // Added by me
        ob_start(); // Added by me
        header('Content-Disposition: attachment;filename="表单数据.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
}